<?php
/**
* $Id: UserIdentity.php 2166 2009-11-12 07:20:12Z phong.quach $
*
* Class UserIdentity
*
* Authenticate user using database. The 'Users' table must have these three columns
* - Username
* - Password
* - Status
* By default, it assumes to use table 'users' in the database but you can choose
* other table by setting UserModel property. This is helpful in case of a site has
* member table and admin table separately
*
* @author Hung Nguyen, Flexica Solution
*/
class UserIdentity extends CUserIdentity
{
    const ERROR_STATUS_INVALID = 3;
    const ERROR_NOT_ALLOWED = 4;

    /**
    * Model class that map to the table in database contain user data
    * This class must be located in folder models of module User
    *
    * @var string
    */
    public $UserModel = 'User';


    protected $user;

    /**
    * Database authentication with checking of user status to support account activation
    * which use Status as Role
    */
//    public function authenticate($statuses = null){
    public function authenticate(){
        $usermodel = yii::createComponent('User.models.'.$this->UserModel);
        $this->user = $usermodel->findByAttributes(array('Email' => $this->username));

        if (is_null($this->user)){
            $this->errorCode = self::ERROR_USERNAME_INVALID;
            Yii::app()->getErrorHandler()->logError($this->getErrorMessage($this->errorCode));
        }elseif($this->user->Password != md5($this->password)){
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
            Yii::app()->getErrorHandler()->logError($this->getErrorMessage($this->errorCode));
        }elseif($this->user->Status <= 0){
            $this->errorCode = self::ERROR_STATUS_INVALID;
            Yii::app()->getErrorHandler()->logError($this->getErrorMessage($this->errorCode));
        }else{
//            if ($statuses == null)
                $this->errorCode = self::ERROR_NONE;
//            else{
//                if (in_array($this->user->Status, $statuses))
//                    $this->errorCode = self::ERROR_NONE;
//                else{
//                    $this->errorCode = self::ERROR_NOT_ALLOWED;
//                    Yii::app()->getErrorHandler()->logError($this->getErrorMessage($this->errorCode));
//                }
//            }
        }

        if ($this->errorCode == self::ERROR_NONE)
            $this->setUserState();


        return $this->user;
    }

    /**
    * Store more user information into his Session
    * You can modify this function to meet your need
    */
    public function setUserState(){
        $this->setState('Username', $this->user->Username);
        $this->setState('Email', $this->user->Email);
        $this->setState('Status', $this->user->Status);
        
        //Set roles
        $roles = explode(',',$this->user->roles);
        $stateRoles = array();
        foreach($roles as $role){
            $stateRoles[] = $role;
        }
        $this->setState('Roles', $stateRoles);
    }

    /**
    * Translate login error code into English message
    *
    * @param mixed $code
    * @return mixed
    */
    public function getErrorMessage($code){
        switch($code){
            case self::ERROR_NONE:
                return '';
            case self::ERROR_USERNAME_INVALID:
                return 'Sorry but your username is not found.';
            case self::ERROR_PASSWORD_INVALID:
                return 'Sorry but your password is not correct';
            case self::ERROR_UNKNOWN_IDENTITY:
                return 'Invalid username or password.';
            case self::ERROR_STATUS_INVALID:
                return 'Your status is inactive. Please contact our administrator.';
            case self::ERROR_NOT_ALLOWED:
                return 'You do not have enough previlege to access the requested page.';
            default:
                return 'Sorry, you cannot login as some errors occur.';
        }
    }

    /**
    * Return User's Id instead of username
    *
    * CWebUser use this Id value to store as app()->user->id.
    * By overriding this function, the app()->user object is very similar to user object queried from database
    *
    */
    public function getId(){
        return $this->user->Id;
    }
}
?>
